package cn.t222.controller.sys;

import cn.t222.pojo.Result;
import cn.t222.pojo.SysUser;
import cn.t222.service.UserService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/sys")
public class UserController {
    @Resource
    private UserService userService;

    @PostMapping("/user/add")
    public Result add(SysUser user) {
        userService.add(user);
        return Result.ok("200","success", null);
    }
    @GetMapping("/user/del")
    public Result del(Integer id) {
        userService.del(id);
        return Result.ok("200","success", null);
    }
    @PostMapping("/user/update")
    public Result update(SysUser user) {
        userService.update(user);
        return Result.ok("200","success", null);
    }
    @GetMapping("/user/get")
    public Result get(Integer id) {
        return Result.ok("200","success", userService.get(id));
    }
    @GetMapping("/user/list")
    public Result getAll(@RequestParam(defaultValue = "1") Integer page,
                         @RequestParam(defaultValue = "10") Integer limit) {
        PageHelper.startPage(page, limit);
        List<SysUser> list = userService.getAll();
        PageInfo<SysUser> pageInfo = PageInfo.of(list);
        return Result.ok("200","success", pageInfo);
    }
}
